package com.smedi.ismedi.notification.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.MailTemplate;
import com.smedi.ismedi.notification.core.domain.repository.MailTemplateRepository;
import com.smedi.ismedi.notification.core.port.adapter.persistence.model.MailTemplateEntity;
import com.smedi.ismedi.notification.core.port.adapter.persistence.mybatis.mapper.MailTemplateMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * @className: MailTemplateRepositoryImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/11/23 14:09
 */

@Slf4j
@Repository
@AllArgsConstructor
public class MailTemplateRepositoryImpl extends ServiceImpl<MailTemplateMapper, MailTemplateEntity> implements MailTemplateRepository, IService<MailTemplateEntity> {

    private final DozerUtil dozerUtil;

    @Override
    public MailTemplate selectByMailTemplateId(Long mailTemplateId) {
        MailTemplateEntity entity = this.getBaseMapper().selectById(mailTemplateId);
        return dozerUtil.convertor(entity, MailTemplate.class);
    }

    @Override
    public MailTemplate selectByMailTemplateName(String mailTemplateName) {
        LambdaQueryWrapper<MailTemplateEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MailTemplateEntity::getTemplateName,mailTemplateName);
        MailTemplateEntity mailTemplateEntity = this.baseMapper.selectOne(lambdaQueryWrapper);
        return dozerUtil.convertor(mailTemplateEntity, MailTemplate.class);
    }

    @Override
    public MailTemplate doInsert(MailTemplate data) {
        MailTemplateEntity entity = dozerUtil.convertor(data, MailTemplateEntity.class);
        this.getBaseMapper().insert(entity);
        return dozerUtil.convertor(entity, MailTemplate.class);
    }

    @Override
    public MailTemplate doUpdate(MailTemplate data) {
        MailTemplateEntity entity = dozerUtil.convertor(data, MailTemplateEntity.class);
        this.getBaseMapper().updateById(entity);
        return dozerUtil.convertor(entity, MailTemplate.class);
    }

    @Override
    public void doDelete(Long mailTemplateId) {
        this.getBaseMapper().deleteById(mailTemplateId);
    }
}